package com.tianyu.inspection.controller;

import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.common.enums.Role;
import com.tianyu.inspection.common.annotation.Permission;
import com.tianyu.inspection.entity.GeneralFacilitiesType;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.entity.vo.Ro;
import com.tianyu.inspection.entity.vo.ServiceRo;
import com.tianyu.inspection.service.api.IGeneralFacilitiesTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 通用设施接口
 * @Date 2021/1/15 - 18:40
 **********************************/

@RestController
@RequestMapping("/api/2.0/generalFacilitiesType")
public class GeneralFacilitiesTypeController {

    @Resource
    private IGeneralFacilitiesTypeService generalFacilitiesTypeService;

    @GetMapping(value = "/list")
    @Permission(value = 4, tags = Constants.API_PATROL)
    public Ro<Object> getList(HttpServletRequest request, @RequestHeader("u-projectId") Integer projectId) {
        QueryMo queryMo = QueryMo.getQueryMo(request);
        int roleId = (int) request.getAttribute("roleId");
        Ro<Object> ro = new Ro<>();
        queryMo.build(GeneralFacilitiesType.class, null);

        if (roleId == Role.ROLE_SUPER_ADMIN.getValue()) {
            return ro.setContent(generalFacilitiesTypeService.searchListPage(queryMo));
        } else {
            return ro.setContent(generalFacilitiesTypeService.searchList(queryMo, projectId));
        }
    }

    @PostMapping(value = "/admin/insert")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<GeneralFacilitiesType> insert(@RequestBody OperateMo operateMo) {
        Ro<GeneralFacilitiesType> ro = new Ro<>();
        operateMo.putIfAbsent("icon", "/static/image/icon/infrastructure/sheshi.png");
        operateMo.createBuild(GeneralFacilitiesType.class, null);
        if (operateMo.getEntity().size() == 0) {
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        if (!generalFacilitiesTypeService.insert(operateMo)) {
            ro.setResultEnum(RoState.FAIL);
        }
        return ro;
    }

    @PatchMapping(value = "/admin/modify")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<GeneralFacilitiesType> modify(@RequestBody OperateMo operateMo) {
        Ro<GeneralFacilitiesType> ro = new Ro<>();
        final ServiceRo serviceRo = operateMo.updateBuild(GeneralFacilitiesType.class, null);
        if (!serviceRo.getResult()) {
            return ro.setResultEnum(RoState.FAIL).setMessage(serviceRo.getMessage());
        }
        if (operateMo.getEntity().size() == 0) {
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        if (Objects.isNull(operateMo.get("id"))) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[id]字段,请传参后重试");
        }
        if (!generalFacilitiesTypeService.update(operateMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("参数无匹配,修改失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<GeneralFacilitiesType> delete(@PathVariable Integer id) {
        Ro<GeneralFacilitiesType> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(GeneralFacilitiesType.class, null);
        if (!generalFacilitiesTypeService.delete(queryMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/logical/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<GeneralFacilitiesType> logicallyDelete(@PathVariable Integer id) {
        Ro<GeneralFacilitiesType> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(GeneralFacilitiesType.class, null);
        if (!generalFacilitiesTypeService.logicallyDelete(queryMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }
}
